package com.hui.utils;

import com.hui.exception.ErrorInfo;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;

@ApiModel("返回结果")
@Data
public class Result<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    public static final int SUCCESS_CODE = 200;

    public static final int FAIL = 1;

    @ApiModelProperty("描述信息")
    private String message;

    @ApiModelProperty("编码：0表示成功，其他值表示失败")
    private Integer code;

    @ApiModelProperty("返回实体")
    private T data;

    public Result() {
        super();
        this.message = "";
        this.code = SUCCESS_CODE;
    }

    public Result(T data) {
        super();
        this.message = "";
        this.code = SUCCESS_CODE;
        this.data = data;
    }

    public Result(ErrorInfo errorInfo) {
        super();
        this.message = errorInfo.getDesc();
        this.code = errorInfo.getCode();
    }

    public Result(T data, ErrorInfo errorInfo) {
        super();
        this.data = data;
        this.message = errorInfo.getDesc();
        this.code = errorInfo.getCode();
    }

    public Result(T data, String msg) {
        super();
        this.code = SUCCESS_CODE;
        this.data = data;
        this.message = msg;
    }

    public Result(Throwable e) {
        super();
        this.message = e.getMessage();
        this.code = FAIL;
    }

    public Result(Integer code, String message, T data) {
        super();
        this.message = message;
        this.code = code;
        this.data = data;
    }

    public static <T> Result<T> success() {
        return new Result<>();
    }

    public static <T> Result<T> success(T data) {
        return new Result<>(data);
    }

    public static <T> Result<T> success(T data, String message) {
        return new Result<>(data, message);
    }

    public static <T> Result<T> error(ErrorInfo errorInfo) {
        return new Result<>(errorInfo);
    }

    public static <T> Result<T> error(T data, ErrorInfo errorInfo) {
        return new Result<>(data, errorInfo);
    }

    public static <T> Result<T> error(Integer code, String message, T data) {
        return new Result<>(code, message, data);
    }

    public static Result<Object> error() {
        return new Result<>(FAIL, "处理失败");
    }

    public static Result getFailRs(Integer code, String errMg) {
        Result failRes = new Result();
        failRes.setCode(code);
        failRes.setMessage(errMg);
        return failRes;
    }
}
